package com.example.passport.service;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.passport.dto.*;
import com.example.passport.entity.UserRole;
import com.example.passport.mapper.UserRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserRoleService {
    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    /**
     * 新增
     */
    public void createUserRole(UserRole userRole) {
        if(userService.findUserById(userRole.getUserId()).getId()==null){
            throw new BusinessException("对象用户不存在");
        }
        if(roleService.findById(userRole.getRoleId(),userRole.getTenantId())==null){
            throw new BusinessException("对象角色不存在");
        }

        if(userRoleMapper.findByUserRoleId(userRole)!=null){
            throw new BusinessException("请入重复添加");
        }
        userRoleMapper.create(userRole);
    }

    /**
     * 列出列表
     */
    public ListDto<UserRoleDto> list(UserRoleDto condition,Pagination pagination){
        if(roleService.findById(condition.getRoleId(),condition.getTenantId())==null){
            throw new BusinessException("对象角色不存在");
        }
        long total = userRoleMapper.count(condition);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }
        pagination.setTotal(total);

        List<UserRole> userRoles = userRoleMapper.findAll(condition,pagination);

        List<UserRoleDto> userRoleDtoList = userRoles.stream().map(userRole -> {
            UserRoleDto userRoleDto = new UserRoleDto();
            BeanUtils.copyProperties(userRole, userRoleDto);
            userRoleDto.setUserName(userService.findUserById(userRole.getUserId()).getName());
            userRoleDto.setRoleName(roleService.findById(userRole.getRoleId(),userRole.getTenantId()).getName());
            return userRoleDto;
        }).collect(Collectors.toList());

        return new ListDto<>(userRoleDtoList, pagination);
    }

    /**
     *修改数据
     */
    public void update(UserRole userRole) {
        if(roleService.findById(userRole.getRoleId(),userRole.getTenantId())==null){
            throw new BusinessException("对象角色不存在");
        }
        if(userRoleMapper.findByUserRoleId(userRole)!=null){
            throw new BusinessException("该用户已存在与该角色下");
        }
        userRoleMapper.update(userRole);
    }

    /**
     *删除数据
     */

    public void delete(UserRole userRole) {
        userRoleMapper.deleteById(userRole.getId(),userRole.getTenantId());
    }


    /**
     *查询组id
     */
    public List<UserRole> findById (Long userId){
        UserRole userRole =UserRole.builder().userId(userId).build();
        List<UserRole> userRoleList = userRoleMapper.findById(userRole);
        return userRoleList;
    }
}